<template>
  <div class="app-container" v-loading="loading.getPageList">
    <div>
      <el-button type="primary" size="normal" @click="getPageList"
        >刷新</el-button
      >
    </div>
    <br />

    <el-table
      :data="tableData"
      :border="true"
      style="width: 100%"
      highlight-current-row
    >
      <el-table-column type="index" label="序号" width="60"> </el-table-column>
      <el-table-column prop="name" label="标题"></el-table-column>
      <el-table-column prop="workflowName" label="流程名称" width="250">
        <template #default="scope">
          <span>{{ scope.row.workflowId.workflowName }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="workflowName" label="处理方式" width="120">
        <template #default="scope">
          <span>{{
            scope.row.workStepType == 0 ? "审批" : "查看（抄送）"
          }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="workflowName" label="是否已读" width="100">
        <template #default="scope">
          <el-link :underline="false" v-if="scope.row.isRead" type="success"
            >已读</el-link
          >
          <el-link :underline="false" v-else type="danger">未读</el-link>
        </template>
      </el-table-column>
      <el-table-column
        prop="creationTime"
        :formatter="dateFormat"
        label="创建时间"
        width="160"
      >
      </el-table-column>
      <el-table-column
        prop="modifiedTime"
        :formatter="dateFormat"
        label="更新时间"
        width="160"
      >
      </el-table-column>

      <el-table-column fixed="right" label="操作" width="150">
        <template #default="scope">
          <el-button
            v-if="scope.row.workflowStatus != 0"
            @click="onViewTask(scope.row)"
            type="text"
            size="normal"
            >{{ scope.row.workStepType == 0 ? "办理" : "查看" }}</el-button
          >
          <el-button
            @click="viewFlow(scope.row)"
            type="text"
            size="normal"
            >流程图</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <br />
    <el-pagination
      background
      layout="sizes,prev, pager, next,total,jumper"
      :total="page.total"
      :page-size="page.maxResultCount"
      :current-page="page.currentPage"
      @current-change="(current) => paginationChange(null, current)"
      @size-change="(size) => paginationChange(size)"
    >
    </el-pagination>
    <process-drawer
      v-model="viewTask.visible"
      :task-id="viewTask.taskId"
    ></process-drawer>
    <FlowDesignForm
      v-model:visible="viewFlowData.visible"
      :workTaskId="viewFlowData.taskId"
      :viewOnly="viewFlowData.viewOnly"
    />
  </div>
</template>

<script>
import { useWorkTaskStore } from "@/stores/workTask";
import { formatToDateTime } from "@/utils/dateUtil";
import ProcessDrawer from "@/components/WorkTasks/ProcessDrawer.vue";
import FlowDesignForm from "@/components/WorkflowDesign/FlowDesignForm.vue";
export default {
  components: { ProcessDrawer,FlowDesignForm },
  data() {
    return {
      page: {
        currentPage: 1,
        maxResultCount: 10,
        skipCount: 0,
        total: 0,
      },
      loading: {
        getPageList: false,
      },
      viewTask: {
        taskId: "",
        visible: false,
      },
      viewFlowData: {
        taskId: "",
        visible: false,
        viewOnly: false,
      },
    };
  },
  mounted() {
    this.getPageList();
  },
  methods: {
    getPageList() {
      this.loading.getPageList = true;
      this.page.skipCount =
        (this.page.currentPage - 1) * this.page.maxResultCount;
      useWorkTaskStore()
        .getUnHandledWorkTasksOfUser(this.page)
        .then((res) => {
          this.tableData = res.items;
          this.page.total = res.total;
          this.loading.getPageList = false;
        });
    },
    dateFormat(row, column) {
      return formatToDateTime(row[column.property]);
    },
    paginationChange(size, current) {
      if (size) this.page.maxResultCount = size;
      if (current) this.page.currentPage = current;
      this.getPageList();
    },
    onViewTask(row) {
      this.viewTask.taskId = row.id;
      this.viewTask.visible = true;
    },
    viewFlow(row) {
      this.viewFlowData.taskId = row.id;
      this.viewFlowData.visible = true;
      this.viewFlowData.viewOnly = true;
    },
  },
};
</script>

<style></style>
